package yun.common.http.response;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import yun.common.http.request.Request;

public abstract class XmlResponseFactory implements IResponseFactory {
	@Override
	public final IResponse newResponse(Request request, HttpResponse response) {
		HttpEntity entity = response.getEntity();
		Header contentTypes = entity.getContentType();
		if (!contentTypes.getValue().contains("text/xml")) {
			return new ErrorResponse(ErrorResponse.UNMATCHED_CONTENT_TYPE);
		}

		String jsonString = null;
		try {
			jsonString = EntityUtils.toString(entity);
		} catch (Exception e) {
			return new ErrorResponse(ErrorResponse.UNKNOWN, e);
		}

		return newJSONResponse(request, jsonString);
	}

	protected abstract IResponse newJSONResponse(Request request, String xmlString);
}
